package com.example.taobaou.ui.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaou.R;
import com.example.taobaou.model.domain.SelectedPage;
import com.example.taobaou.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectedPageLiftAdapter extends RecyclerView.Adapter<SelectedPageLiftAdapter.InnerHolder> {

    List<SelectedPage.DataBean> mData = new ArrayList<>();
    int currentSelectedPosition = 0;
    private onLeftItemClickListener mItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_lift, parent, false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        TextView itemTv = holder.itemView.findViewById(R.id.left_category);
        if (currentSelectedPosition == position) {
            itemTv.setBackgroundColor(itemTv.getResources().getColor(R.color.colorEFEEEE));
        } else {
            itemTv.setBackgroundColor(Color.WHITE);
        }
        SelectedPage.DataBean dataBean = mData.get(position);
        itemTv.setText(dataBean.getFavorites_title());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null || currentSelectedPosition != position){
                    currentSelectedPosition = position;
                    mItemClickListener.leftItemClick(dataBean);
                    notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectedPage categories) {
        List<SelectedPage.DataBean> data = categories.getData();
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
        if (mData.size() != 0){
            mItemClickListener.leftItemClick(mData.get(currentSelectedPosition));
        }


    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setOnLeftItemClickListener(onLeftItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface onLeftItemClickListener {
        void leftItemClick(SelectedPage.DataBean item);
    }
}
